Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    selectType: {
      type: String,
      value: 'select'
    },
    skuList: {
      type: Array,
      value: []
    },
    currentGoods: {
      type: Object,
      value: null
    },
    skuCount: {
      type: Number,
      value: 1
    },
    goodsList: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    numberValue: 0,
    selectedList: [],
    goodsListSkus: [],
    selectedListTypes: [],
    validateSkuStatus: false,
    validateSkuStatusMsg: '',
    selectSkuName: ''
  },
  lifetimes: {
    ready () {
      if (this.data.currentGoods) {
        let skuList = [...this.data.skuList]
        for (let i = 0; i < this.data.currentGoods.SkuSelectionIDList.length; i++) {
          let indexs = this.getSkuIndexById(this.data.currentGoods.SkuSelectionIDList[i])
          skuList[indexs.skusTypeIndex].selected = null
          skuList[indexs.skusTypeIndex].skuIndex = null
          this.setData({
            skuList: skuList
          })
          this.clickSkuItem({
            detail: {
              skuId: this.data.currentGoods.SkuSelectionIDList[i],
              skuTypeIndex: indexs.skusTypeIndex,
              skuIndex: indexs.skusIndex
            }
          })
        }
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getSkuIndexById(skuId) {
      let skuList = this.data.skuList
      for (let i = 0; i < skuList.length;i++) {
        for (let j = 0; j < skuList[i].SkuSelectionList.length; j++) {
          if (skuList[i].SkuSelectionList[j].Sku_SelectionID === skuId) {
            return {
              skusTypeIndex: i,
              skusIndex: j
            }
          }
        }
      }
    },
    clickSkuItem ({detail}) {
      let skuList = [...this.data.skuList]
      if (skuList[detail.skuTypeIndex].selected === detail.skuId) {
        skuList[detail.skuTypeIndex].selected = null
        skuList[detail.skuTypeIndex].skuIndex = null
      } else {
        skuList[detail.skuTypeIndex].selected = detail.skuId
        skuList[detail.skuTypeIndex].skuIndex = detail.skuIndex
      }
      this.setData({
        skuList: skuList
      })
      this.computedList()
    },
    computedList() {
      let selectedList = []
      let selectedListTypes = []
      if (this.data.skuList && this.data.skuList.length > 0) {
        for (let i = 0; i < this.data.skuList.length; i++) {
          if (this.data.skuList[i].selected) {
            selectedList.push(this.data.skuList[i].selected)
            selectedListTypes.push(this.data.skuList[i])
          }
        }
      }
      let all = this.getGoodsBySelected(selectedList)
      for (let i = 0; i < selectedList.length; i++) {
        let selectedListCopy = [...selectedList]
        let selectedListTypesCopy = [...selectedListTypes]
        selectedListCopy.splice(selectedListCopy.indexOf(selectedList[i]), 1)
        let all2 = this.getGoodsBySelected(selectedListCopy)
        selectedListTypesCopy = selectedListTypesCopy.splice(selectedListTypesCopy.indexOf(selectedListTypes[i]), 1)
        let xx = []
        for (let j = 0; j < selectedListTypesCopy.length; j++) {
          for (let k = 0; k < selectedListTypesCopy[j].SkuSelectionList.length; k++) {
            xx.push(selectedListTypesCopy[j].SkuSelectionList[k].skuId)
          }
        }
        let a = []
        for (let j = 0; j < all2.length; j++) {
          if (all.indexOf(all2[j]) < 0 && xx.indexOf(all2[j]) >= 0) {
            a.push(all2[j])
          }
        }
        all = [
          ...all,
          ...a
        ]
      }
      this.setData({
        goodsListSkus: all,
        selectedListTypes: selectedListTypes,
        selectedList: selectedList
      })
      this.validateSku()
    },
    validateSku () {
      let skuList = this.data.skuList
      let flag = true
      let strs = []
      for (let i = 0; i < skuList.length; i++ ) {
        if (!skuList[i].selected) {
          strs.push(skuList[i].Goods_Sku_Group_ItemName)
          flag = false
        }
      }
      this.setData({
        validateSkuStatus: flag,
        validateSkuStatusMsg: strs.join(' / ')
      })
      this.setData({
        selectSkuName: this.selectSkuName()
      })
      if (flag) {
        this.getGoods()
        this.change()
      }
    },
    selectSkuName() {
      let selectedListTypes = this.data.selectedListTypes
      let skuList = this.data.skuList
      let names = []
      for (let i = 0; i < selectedListTypes.length; i++) {
        names.push(selectedListTypes[i].SkuSelectionList[selectedListTypes[i].skuIndex].Sku_SelectionName)
      }
      return names.join(' / ')
    },
    getGoods() {
      let selectedList = this.data.selectedList
      if (this.data.goodsList && this.data.goodsList.length > 0) {
        for (let i = 0; i < this.data.goodsList.length; i++) {
          if (this.isContained(this.data.goodsList[i].SkuSelectionIDList, selectedList)) {
            this.setData({
              currentGoods: this.data.goodsList[i]
            })
          }
        }
      }
    },
    /**
     * 通过已选择的规格获取符合条件的商品
     */
    getGoodsBySelected(selectedList) {
      let goodsListSkus = []
      if (this.data.goodsList && this.data.goodsList.length > 0) {
        for (let i = 0; i < this.data.goodsList.length; i++) {
          if (this.data.goodsList[i].Goods_Sku_InfoStock > 0 && this.isContained(this.data.goodsList[i].SkuSelectionIDList, selectedList)) {
            goodsListSkus = [
              ...goodsListSkus,
              ...this.data.goodsList[i].SkuSelectionIDList
            ]
          }
        }
      }
      
      return goodsListSkus
    },
    isContained (aa, bb) {
      if(!(aa instanceof Array) || !(bb instanceof Array) || ((aa.length < bb.length))) {
        return false;
      }
      for (var i = 0; i < bb.length; i++) {
        var flag = false;
        for (var j = 0; j < aa.length; j++) {
          if (aa[j] == bb[i]) {
            flag = true;
            break;
          }
        }
        if (flag == false) {
          return flag;
        }
      }
      return true;
    },
    selectSkuItem({ currentTarget }) {
      this.skuChange(currentTarget.dataset.skuitem)
    },
    skuChange (skuitem) {
      this.setData({
        currentSku: skuitem
      })
      this.change()
    },
    change() {
      this.triggerEvent("change", {
        currentGoods: {
          ...this.data.currentGoods,
          selectSkuName: this.selectSkuName()
        },
        count: this.data.skuCount
      })
    },
    handleChange ({detail}) {
      this.setData({
        skuCount: detail.value
      })
      this.change()
    },
    closeModel () {
      this.triggerEvent('close')
    },
    addToShoppingCart () {
      this.triggerEvent('addToShoppingCart')
    }
  }
})
